package com.bw.android.bwgao6week2a.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import androidx.fragment.app.Fragment;

import com.bw.android.bwgao6week2a.AppInstance;
import com.bw.android.bwgao6week2a.R;
import com.bw.android.bwgao6week2a.databinding.FragmentAnimationBinding;
import com.bw.ch.baselib.BaseBindingFragment;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class AnimationFragment extends BaseBindingFragment<FragmentAnimationBinding> {
    private AnimatorSet animatorSet;

    public AnimationFragment() {
        super(true);
    }
    @Override
    public int getLayout() {
        return R.layout.fragment_animation;
    }

    @SuppressLint("ObjectAnimatorBinding")
    @Override
    public void initView() {
        Log.d(AppInstance.TAG, "动画Fragment initView");
        //利用AnimatorSet和ObjectAnimator实现缩放动画
        animatorSet = new AnimatorSet();
        animatorSet.playTogether(
                //透明度动画
                ObjectAnimator.ofFloat(mBinding.ivAnimationA, "alpha", 1, 0, 1).setDuration(3000),
                //旋转动画
                ObjectAnimator.ofFloat(mBinding.ivAnimationA, "rotation", 0, 360).setDuration(3000),
                //X轴从1倍放大到2倍，再恢复到1倍
                ObjectAnimator.ofFloat(mBinding.ivAnimationA, "scaleX", 1, 0, 1).setDuration(3000),
                //Y轴从1倍放大到2倍，再恢复到1倍
                ObjectAnimator.ofFloat(mBinding.ivAnimationA, "scaleY", 1, 0, 1).setDuration(3000)
        );
        mBinding.btnPlayAnimationA.setOnClickListener(v -> {
            //设置动画的轴心为中心点
            mBinding.ivAnimationA.setPivotX(mBinding.ivAnimationA.getWidth() / 2);
            mBinding.ivAnimationA.setPivotY(mBinding.ivAnimationA.getHeight() / 2);
            //开始播放动画
            animatorSet.start();
        });


        // 以下是使用XML(res/anim/alpha_anim.xml)实现动画的方式
        Animation animation = AnimationUtils.loadAnimation(this.getContext(), R.anim.alpha_anim);
        mBinding.btnPlayAnimationB.setOnClickListener(v -> {
            //播放动画
            mBinding.ivAnimationB.startAnimation(animation);
        });

    }

    @Override
    public void initData() {

    }
}